<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="number"
          placeholder="订单编号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        状态：
        <el-select v-model="zt" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        类型：
        <el-select v-model="type" placeholder="请选择">
          <el-option
            v-for="item in typelist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        prop="mName"
        header-align="center"
        align="center"
        width="80"
        label="酒店名称"
      >
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="用户姓名"
      >
      </el-table-column>
      <el-table-column
        prop="money"
        header-align="center"
        align="center"
        label="金额"
      >
      </el-table-column>
       <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="类型"
      >
      <template slot-scope="scope">
          <div v-if="scope.row.type == 1">酒店</div>
          <div v-if="scope.row.type == 2">民宿</div>
          <div v-if="scope.row.type == 3">文创</div>
          <div v-if="scope.row.type == 4">餐饮</div>
          <div v-if="scope.row.type == 5">票务</div>
        </template>
      </el-table-column>
       <el-table-column
        prop="cjTime"
        header-align="center"
        align="center"
        label="下单时间"
      >
      </el-table-column>
      <el-table-column
        prop="zt"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.zt == 0">未支付</div>
          <div v-if="scope.row.zt == 1">已付款</div>
          <div v-if="scope.row.zt == 1 && scope.row.type == 1 ">确认中</div>
          <div v-if="scope.row.zt == 1 && scope.row.type == 3">待发货</div>
          <div v-if="scope.row.zt == 2">取消</div>
          <div v-if="scope.row.zt == 3">用户申请退款</div>
          <div v-if="scope.row.zt == 4">已完成 </div>
          <div v-if="scope.row.zt == 5">评价完成</div>
          <div v-if="scope.row.zt == 6">确定完成</div>
          <div v-if="scope.row.zt == 7">退款完成 </div>
          <div v-if="scope.row.zt == 8">商家申请退款</div>
          <div v-if="scope.row.zt == 9">已发货</div>
          <div v-if="scope.row.zt == 10">已拒绝退款</div>
          <div v-if="scope.row.zt == 11">已同意退款</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="number"
        header-align="center"
        align="center"
        label="订单编号"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
          v-if="isAuth('order:user:info')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >详情</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.zt == 3 && isAuth('order:user:refund')"
            @click="tyBnt(scope.row.id)"
            >同意退款</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.zt == 3 && isAuth('order:user:norefund')"
            @click="jjBnt(scope.row.id)"
            >拒绝退款</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.zt == 1 && scope.row.type == 3 && isAuth('order:user:deliver')"
            @click="addOrAddsHandle(scope.row.id)"
            >发货</el-button
          >
          <!-- <el-button
            type="text"
            size="small"
            v-if="scope.row.zt == 1 && scope.row.type == 3"
            @click="qdshBnt(scope.row.id)"
            >确定收货</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <!-- 发货弹窗 -->
    <add-or-add
      v-if="addOrAddVisible"
      ref="addOrAdds"
      @refreshDataList="getDataList"
    ></add-or-add>
  </div>
</template>

<script>
import AddOrUpdate from "./order-add-or-update";
import AddOrAdd from "./order-add-or-add";
export default {
  data() {
    return {
      dataForm: {
      },
      number:'',
      zt: '',
      type: '',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addOrAddVisible: false,
      typelist: [
        {
          value: "1",
          label: "酒店",
        },
        {
          value: "2",
          label: "民宿",
        },
        {
          value: "3",
          label: "文创",
        },
        {
          value: "4",
          label: "餐饮",
        },
        {
          value: "5",
          label: "票务",
        }
      ],
      options: [
        {
          value: "0",
          label: "未支付",
        },
        {
          value: "1",
          label: "已付款",
        },
        {
          value: "2",
          label: "取消",
        },
        {
          value: "3",
          label: "用户申请退款",
        },
        {
          value: "4",
          label: "已完成",
        },
        {
          value: "5",
          label: "评价完成",
        },
        {
          value: "6",
          label: "确定完成",
        },
        {
          value: "7",
          label: "退款完成",
        },
        {
          value: "8",
          label: "商家申请退款",
        },
        {
          value: "9",
          label: "已发货",
        }
      ],
    };
  },
  components: {
    AddOrUpdate,
    AddOrAdd
  },
  activated() {
    this.getDataList();
  },
  methods: {
     // 同意申请退款
     tyBnt(id) {
      this.$confirm('此操作将同意申请退款, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
          url: this.$http.adornUrl("sys/order/tkUpdate"),
          method: "post",
          params: this.$http.adornParams({
            id: id,
            tkZt : 2,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.$message({
            type: 'success',
            message: '操作成功!'
          });
          } else {
            this.$message({
            type: 'info',
            message: '操作失败'
          });  
          }
        });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    // 拒绝申请退款
    jjBnt(id) {
      this.$confirm('此操作将拒绝申请退款, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
          url: this.$http.adornUrl("sys/order/tkUpdate"),
          method: "post",
          params: this.$http.adornParams({
            id: id,
            tkZt : 3,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.$message({
            type: 'success',
            message: '操作成功!'
          });
          } else {
            this.$message({
            type: 'info',
            message: '操作失败'
          });  
          }
        });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    // qdshBnt(id) {
    //   this.$confirm('此操作将确认收货, 是否继续?', '提示', {
    //       confirmButtonText: '确定',
    //       cancelButtonText: '取消',
    //       type: 'warning'
    //     }).then(() => {
    //       this.$http({
    //       url: this.$http.adornUrl("sys/order/tytk"),
    //       method: "post",
    //       params: this.$http.adornParams({
    //         id: id,
    //         tkZt : 3,
    //       }),
    //     }).then(({ data }) => {
    //       if (data && data.code === 0) {
    //         this.getDataList()
    //         this.$message({
    //         type: 'success',
    //         message: '操作成功!'
    //       });
    //       } else {
    //         this.$message({
    //         type: 'info',
    //         message: '操作失败'
    //       });  
    //       }
    //     });
    //     }).catch(() => {
    //       this.$message({
    //         type: 'info',
    //         message: '已取消'
    //       });          
    //     });
    // },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("sys/order/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          number: this.number,
          zt: this.zt,
          type: this.type
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page;
          this.totalPage = data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 发货
    addOrAddsHandle(id) {
      this.addOrAddVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrAdds.init(id);
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      console.log(id)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("sys/creativegoods/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
